package com.breeze.webview;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.breeze.base.loadsir.ErrorCallback;
import com.breeze.base.loadsir.LoadingCallback;
import com.breeze.webview.callbacks.WebViewCallBack;
import com.breeze.webview.databinding.FragmentWebviewBinding;
import com.breeze.webview.utils.Constants;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

public class WebViewFragment extends Fragment implements WebViewCallBack, OnRefreshListener {
    private static final String TAG = "WebViewFragment";
    private String mUrl;
    private boolean mCanNativeRefresh;
    private FragmentWebviewBinding mBinding;
    private LoadService mLoadService;

    public static WebViewFragment newInstance(String url, boolean canNativeRefresh) {

        Bundle args = new Bundle();

        WebViewFragment fragment = new WebViewFragment();
        args.putString(Constants.URL, url);
        args.putBoolean(Constants.CAN_NATIVE_REFRESH, canNativeRefresh);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mUrl = bundle.getString(Constants.URL);
            mCanNativeRefresh = bundle.getBoolean(Constants.CAN_NATIVE_REFRESH);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_webview, container, false);
        mBinding.webview.registerWebViewCallback(this);
        mBinding.webview.loadUrl(mUrl);
        mBinding.smartrefreshlayout.setOnRefreshListener(this);
        mBinding.smartrefreshlayout.setEnableRefresh(mCanNativeRefresh); // 启用刷新
        mBinding.smartrefreshlayout.setEnableLoadMore(false); // 禁止加载更多

        mLoadService = LoadSir.getDefault().register(mBinding.smartrefreshlayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mBinding.webview.reload();
            }
        });
        return mLoadService.getLoadLayout(); // 注意替换为load layout
    }

    @Override
    public void pageStarted(String url) {
        Log.i(TAG, "pageStarted: ");
        // 显示加载的View
        mLoadService.showCallback(LoadingCallback.class);
    }

    private boolean isError;

    @Override
    public void pageFinished(String url) {
        Log.i(TAG, "pageFinished: ");
        mBinding.smartrefreshlayout.finishRefresh();
        if (isError) {
            mLoadService.showCallback(ErrorCallback.class);
        } else {
            mLoadService.showSuccess();
        }
        isError = false;
    }

    @Override
    public void onError() {
        isError = true;
        Log.i(TAG, "onError: ");
    }

    @Override
    public void updateTitle(String title) {
        Log.i(TAG, "updateTitle: ");
        if (getActivity() instanceof WebViewActivity) {
            ((WebViewActivity) getActivity()).getSupportActionBar().setTitle(title);
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        Log.i(TAG, "onRefresh: ");
        mBinding.webview.reload();
    }
}
